package com.snda.lib.task;

import com.snda.lib.http.HttpConst;
import com.snda.lib.http.ICallBack;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;

/*
 * 从服务器下载文件
 */
public class DownloadFileNotifiedTask extends DownloadAsyncTask {

	protected long nTotalSize = 0;
	protected int nCurrentPercent = 0;

	// 通知栏
	protected NotificationManager updateNotificationManager = null;
	protected Notification updateNotification = null;
	protected PendingIntent updatePendingIntent = null;
	protected Intent notifyIntent = null;

	public DownloadFileNotifiedTask(Context context, ICallBack callback) {
		super(context, callback);

		this.nTaskType = HttpConst.TASKTYPE_DOWNLOADFILE;

		// 开始下载，则此时发送一个通知到通知栏
		updateNotificationManager = (NotificationManager) context
				.getSystemService(Service.NOTIFICATION_SERVICE);
		updateNotification = new Notification();
	}

	public void setPendingIntent(Intent pendingIntent) {
		notifyIntent = pendingIntent;

		if (notifyIntent != null) {
			notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			updatePendingIntent = PendingIntent.getActivity(context, 0,
					notifyIntent, 0);
		}
	}

	/*
	 * 通知当前需要下载的内容的总长度
	 * 
	 * @param nTotalSize 返回的需要下载的总长度
	 */
	@Override
	public void OnGetSize(long nTotalSize) {
		this.nTotalSize = nTotalSize;
	}

	@Override
	public boolean OnStartTask(String strUrl, String strSavePath) {
		int nPercent = 0;

		updateNotificationManager.cancel(nCookie);

		// 设置通知栏显示内容
		updateNotification.icon = android.R.drawable.stat_sys_download;
		updateNotification.tickerText = "正在下载" + strDownloadFileName;

		if (updatePendingIntent != null) {
			updateNotification.setLatestEventInfo(context, strDownloadFileName,
					nPercent + "%", updatePendingIntent);
		} else {
			updateNotification.setLatestEventInfo(context, strDownloadFileName,
					nPercent + "%", null);
		}

		updateNotification.flags = Notification.FLAG_AUTO_CANCEL;
		updateNotificationManager.notify(nCookie, updateNotification);
		return true;
	}

	/*
	 * 通知下载任务开始
	 */
	@Override
	public void OnBeginTask(String strUrl, long nFinishedSize,
			String strSavePath) {

		int nPercent = (int) (nFinishedSize * 100 / nTotalSize);
		if (updatePendingIntent != null) {
			updateNotification.setLatestEventInfo(context, strDownloadFileName,
					nPercent + "%", updatePendingIntent);
		} else {
			updateNotification.setLatestEventInfo(context, strDownloadFileName,
					nPercent + "%", null);
		}
		updateNotificationManager.notify(nCookie, updateNotification);
	}

	/*
	 * 通知当前的下载进度
	 * 
	 * @param nFinishedSize 已经下载的总长度
	 */
	@Override
	public void OnNotifyProcess(long nFinishedSize) {
		publishProgress(nFinishedSize);

		int nPercent = (int) (nFinishedSize * 100 / nTotalSize);
		if (nPercent != nCurrentPercent) {

			if (updatePendingIntent != null) {
				updateNotification.setLatestEventInfo(context,
						strDownloadFileName, (int) nFinishedSize * 100
								/ nTotalSize + "%", updatePendingIntent);
			} else {
				updateNotification.setLatestEventInfo(context,
						strDownloadFileName, (int) nFinishedSize * 100
								/ nTotalSize + "%", null);
			}

			updateNotificationManager.notify(nCookie, updateNotification);
			nCurrentPercent = nPercent;
		}
	}

	/*
	 * 通知当前的任务已经下载完毕
	 */
	@Override
	public void OnFinishedTask(String strDownloadPath) {
		if (strDownloadPath == null)
			return;

		nCurrentPercent = 100;

		updateNotification.icon = android.R.drawable.stat_sys_download_done;
		updateNotification.defaults = Notification.DEFAULT_SOUND;// 铃声提醒
		if (strDownloadFileName != null
				&& strDownloadFileName.trim().equalsIgnoreCase("") != false) {
			updateNotification.setLatestEventInfo(context, strDownloadFileName,
					"下载已完成", updatePendingIntent);
		}
		updateNotificationManager.notify(nCookie, updateNotification);
	}

	/*
	 * 通知当前的任务下载发生失败
	 */
	@Override
	public void OnFailed(int nErrorCode) {
	}
}
